package com.rtx.edi.platform.baseinfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.annotation.RtxDynamicsClass;
import com.rtx.api.enums.FieldValidateType;
import com.rtx.api.utils.FieldValidateUtils;
import com.rtx.edi.model.apibaseinfo.po.PlApibaseinfo;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.baseinfo.bean.ApiStdRequest;
import com.rtx.edi.platform.baseinfo.service.PlApiStdRequestService;
import com.rtx.edi.platform.baseinfo.service.PlApibaseinfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"wms标准报文字段配置"})
@RestController
@RequestMapping("/v1/PlApiStdRequest")
public class PlApiStdRequestController {
	
	@Autowired
	PlApiStdRequestService configService;
	
	@Autowired
	PlApibaseinfoService plApibaseinfoService;
	
	
	
	@ApiOperation(value = "获取报文模板", notes = "获取报文模板")
	@PostMapping("selectSTDDynamicsClass")
	public EdiResponse querySTD(@RequestBody ApiStdRequest dto){
		Map<String,List<Map<String,String>>> options = new HashMap<>();
		List<Map<String,String>> allDylist = new ArrayList<Map<String,String>>();
		Map<String,String> allDy = new HashMap<String, String>();
		allDy.put("lable", "");
		allDy.put("value", "");
		allDylist.add(allDy);
		
		PlApibaseinfo api =	plApibaseinfoService.getById(dto.getApiid());
		
		String mainClassName = Optional.ofNullable(api.getMainclass()).orElse("").split(",")[0];
		String subClassName = Optional.ofNullable(api.getSubclass()).orElse("").split(",")[0];
		
		try {
			Class main = Class.forName(mainClassName);
			Map<String,String> alldy = new HashMap<String, String>();
			RtxDynamicsClass dynamicsClass = (RtxDynamicsClass) main.getDeclaredAnnotation(RtxDynamicsClass.class);
			alldy.put("label", dynamicsClass.name());
			alldy.put("value", api.getMainclass());
			allDylist.add(alldy);
			
			Class sub = Class.forName(subClassName);
			Map<String,String> subdy = new HashMap<String, String>();
			RtxDynamicsClass subClass = (RtxDynamicsClass) sub.getDeclaredAnnotation(RtxDynamicsClass.class);
			subdy.put("label", subClass.name());
			subdy.put("value", api.getSubclass());
			allDylist.add(subdy);
		} catch (Exception e) {
		}
		
		
//		Set<Class<?>> set = RtxLoadClass.getClassSet("com.rtx");
//		for(Class clazz:set) {
//			RtxSTDRequest stdRequest = (RtxSTDRequest) clazz.getDeclaredAnnotation(RtxSTDRequest.class);
//			
//			RtxDynamicsClass dynamicsClass = (RtxDynamicsClass) clazz.getDeclaredAnnotation(RtxDynamicsClass.class);
//			if(dynamicsClass != null) {
//				String mainClassName = Optional.ofNullable(api.getMainclass()).orElse("").split(",")[0];
//				String subClassName = Optional.ofNullable(api.getSubclass()).orElse("").split(",")[0];
//				
//				if(Arrays.asList(mainClassName,subClassName).contains(clazz.getName())) {
//					Map<String,String> alldy = new HashMap<String, String>();
//					alldy.put("label", dynamicsClass.name());
//					alldy.put("value", mainClassName);
//					allDylist.add(alldy);
//				};
//			}
//			
//		}
		
		
		options.put("allDynamsubics", allDylist);
		return EdiResponse.success().data("item", options);
	}
	
	
    /**
     * 获取wms标准报文字段配置列表
     * @return
     */
    @ApiOperation(value = "wms标准报文字段配置列表", notes = "分页获取wms标准报文字段配置列表")
    @PostMapping("selectApiStdRequestPageList")
    public EdiResponse selectApiStdRequestPageList(@RequestBody ApiStdRequest dto){
        Page<ApiStdRequest> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        configService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 获取wms标准报文字段配置
     * @return
     */
    @ApiOperation(value = "获取wms标准报文字段配置", notes = "根据ID获取wms标准报文字段配置")
    @PostMapping("selectPlApiStdRequestById")
    public EdiResponse selectApiStdRequestById(@RequestBody ApiStdRequest dto){
        return EdiResponse.success().data("item",configService.getById(dto.getId()));
    }

    /**
     * 新增wms标准报文字段配置
     * @return
     */
    @ApiOperation(value = "新增wms标准报文字段配置", notes = "新增wms标准报文字段配置")
    @PostMapping("addApiStdRequest")
    public EdiResponse addApiStdRequest(@RequestBody ApiStdRequest dto){
    	try {
            configService.addApiStdRequest("ris_edi", dto);
            
            //激活新增字段
            configService.activeFieldAddCache(Long.parseLong(dto.getApiid()));
            
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
        
    }

    /**
     * 修改wms标准报文字段配置
     * @return
     */
    @ApiOperation(value = "修改wms标准报文字段配置", notes = "根据ID修改wms标准报文字段配置")
    @PostMapping("editApiStdRequest")
    public EdiResponse editExpressApiConfig(@RequestBody ApiStdRequest dto){
    	configService.updateById(dto);
        return  EdiResponse.success();
    }

    /**
     * 删除wms标准报文字段配置
     * @return
     */
    @ApiOperation(value = "删除wms标准报文字段配置", notes = "根据ID删除wms标准报文字段配置")
    @PostMapping("deleteApiStdRequest")
    public EdiResponse deletePlApplicationFieldById(@RequestBody ApiStdRequest dto){
        FieldValidateUtils.fieldNotNull(dto,FieldValidateType.DELETE);
        return  configService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 批量删除wms标准报文字段配置
     * @return
     */
    @ApiOperation(value = "批量删除wms标准报文字段配置", notes = "批量删除wms标准报文字段配置")
    @PostMapping("deleteApiStdRequestIds")
    public EdiResponse deletePlApplicationFieldByIds(@RequestBody List<ApiStdRequest> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  configService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
	

}
